package com.example.test.example.update;

import java.util.HashMap;
import java.util.Map;

import com.example.test.example.BaseExample;
import com.example.test.exception.ExampleException;
import com.example.test.exception.example.NoUpdateFieldException;

public class UpdateBaseExample extends BaseExample {
    private Map<String,Object> updateFields;

    public UpdateBaseExample (String tableName,Map<String, Object> updateFields) {
        super.addTableName(tableName);
        this.updateFields = updateFields;
    }
    public UpdateBaseExample (String tableName) {
        super.addTableName(tableName);
    }

    /**
     *
     * @throws ExampleException
     */
    @Override
    public void end() throws ExampleException {
        super.end();
        //判断更新时是否有传入更新字段，否则抛出异常
        if(updateFields==null ||updateFields.size()==0){
            throw new NoUpdateFieldException("使用UpdateBaseExample时更新字段不能为空！");
        }
    }

    /**
     * 增加字段方法  类似于sql语句的   set   field   =  newValue
     * @param field  字段名
     * @param newValue   新值
     * @return
     */
    public UpdateBaseExample addUpdateField(String field,Object newValue) {
        if(this.updateFields == null) {
            this.updateFields = new HashMap<String,Object>();
        }
        this.updateFields.put(field, newValue);
        return this;
    }



    public void setUpdateFields(Map<String, Object> updateFields) {
        this.updateFields = updateFields;
    }
    public Map<String, Object> getUpdateFields() {
        return updateFields;
    }
    @Override
    public String toString() {
        return "UpdateBaseExample{" +
                "updateFields=" + updateFields +
                '}';
    }
}
